package com.ict.txmsv2.services;

import com.ict.txmsv2.mapper.CheckUserMapper;
import com.ict.txmsv2.mapper.UserMapper;
import com.ict.txmsv2.mapper.UserRoleMapper;
import com.ict.txmsv2.model.CheckUser;
import com.ict.txmsv2.model.User;
import com.ict.txmsv2.model.UserRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-16
 */
@Service
public class CheckService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    CheckUserMapper checkUserMapper;

    public List<CheckUser> getAllCheckUsers() {
        return checkUserMapper.getAllCheckUsers();
    }

    @Transactional(rollbackFor = Exception.class)
    public int changeUserToFormal(Integer id, Integer[] rids) {
        CheckUser checkUser = checkUserMapper.selectByPrimaryKey(id);
        User user = new User();
        BeanUtils.copyProperties(checkUser,user);
        user.setId(null);
        checkUserMapper.changeToSendMailState(id);
        userMapper.insertSelective(user);
        int uid = userMapper.getNewestId();
        userRoleMapper.insertRids(uid,rids);
        return 1;
    }

    public int register(CheckUser checkUser) {
        return checkUserMapper.insertSelective(checkUser);
    }
}
